package com.itcodai.controller.course08;

import com.itcodai.config.exception.BusinessErrorException;
import com.itcodai.entity.BusinessMsgEnum;
import com.itcodai.entity.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * @author c-fanqj
 * @date 2023/3/31
 * @apiNote 全局异常测试
 */
@RestController
@RequestMapping("/exception")
@Api(value = "全局异常处理测试")
public class ExceptionController {

    // 打印日志
    private final Logger logger =
            LoggerFactory.getLogger(this.getClass());

    @GetMapping("/null/point/exception")
    @ApiOperation(value = "空指针异常测试")
    public JsonResult testNullPointException() {
        String str = null;
        str.length();
        return new JsonResult();
    }

    @PostMapping("/test")
    @ApiOperation(value = "参数缺失异常测试")
    public JsonResult test(@RequestParam("name") @ApiParam(value = "用户名") String name,
                           @RequestParam("pass") @ApiParam(value = "用户密码") String pass) {
        logger.info("name：{}", name);
        logger.info("pass：{}", pass);
        return new JsonResult();
    }

    @GetMapping("/otherexception")
    @ApiOperation(value = "其他异常测试")
    public JsonResult otherException() {
         int i = 1 / 0;
        return new JsonResult();
    }

    @GetMapping("/business")
    @ApiOperation(value = "业务异常测试")
    public JsonResult testException() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            logger.info("错误信息：", e);
            throw new BusinessErrorException(BusinessMsgEnum.UNEXPECTED_EXCEPTION);
        }
        return new JsonResult();
    }

}
